package org.shj.tdpl.example.service;

import org.shj.tdpl.example.dao.StaffDAO;
import org.shj.tdpl.example.entity.Staff;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author Shen Huang Jian
 * @date 2019-08-27 18:33
 */
@Service
public class StaffServiceImpl implements StaffService {

    @Autowired
    StaffDAO staffDAO;

    @Override
    public List<Staff> findAllStaff(){
        return staffDAO.findAll();
    }

    @Override
    public int avgAge(){
        List<Staff> staffs = findAllStaff();
        if(CollectionUtils.isEmpty(staffs)){
           return 0;
        }else{
            int sum = staffs.stream().mapToInt(Staff::getAge).sum();
            int size = staffs.size();
            return sum/size;
        }
    }
}
